package Interface;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Sess
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Main.*;
import Process.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class ServerInterface extends JFrame implements ActionListener {
    //ShowCanvas canvas;

    JFrame frame;
    public static int click = 1;
    MainWind main;
    Icon ic = new ImageIcon("images/PL.jpg");
    Icon ic1 = new ImageIcon("images/PP.jpg");
    Border blackline = BorderFactory.createLineBorder(Color.black);
    JLabel t[] = new JLabel[13];
    JLabel o[] = new JLabel[13];
    JLabel p[] = new JLabel[13];
    JLabel me[] = new JLabel[13];
    String tapbai[] = new String[13];
    Hearts control;
    Container container;
    public JPanel panel_;
    public int x1[] = new int[13];
    public int x2[] = new int[13];
    public int x3[] = new int[13];
    public int x4[] = new int[13];
    public int y1[] = new int[13];
    public int y2[] = new int[13];
    public int y3[] = new int[13];
    public int y4[] = new int[13];

    public ServerInterface(Hearts control) {
        this.control = control;
        DrawServerInterface();
        for (int i = 0; i < 13; i++) {
            x1[i] = 580;
            x2[i] = 580;
            x3[i] = 580;
            x4[i] = 580;
            y1[i] = 310;
            y2[i] = 310;
            y3[i] = 310;
            y4[i] = 310;
        }
        this.control.SP= new ServerProcess(control);
    }

    private void DrawServerInterface() {

        // Create the menu bar
        JMenuBar menuBar = new JMenuBar();

        // Create a menu
        JMenu Game = new JMenu("Game");
        menuBar.add(Game);

        JMenu Help = new JMenu("Help");
        menuBar.add(Help);

        // Create a menu item
        JMenuItem Start = new JMenuItem("Start");
        Start.setActionCommand("Start");
        Start.addActionListener(new StartAnimationAction());
        Game.add(Start);

        JMenuItem Exit = new JMenuItem("Exit");
        Exit.setActionCommand("Exit");
        Start.addActionListener(this);
        Game.add(Exit);

        JMenuItem HowtoPlay = new JMenuItem("How to play");
        HowtoPlay.setActionCommand("How");
        Start.addActionListener(this);
        Help.add(HowtoPlay);

        JMenuItem About = new JMenuItem("About Hearts");
        About.setActionCommand("About");
        Start.addActionListener(this);
        Help.add(About);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        panel_ = new MyPanel();
        panel_.setBounds(0, 0, 1278, 770);
        frame.getContentPane().add(panel_);

        frame.setSize(1278, 770);
        frame.setVisible(true);
        frame.setJMenuBar(menuBar);

        
    }

    class StartAnimationAction implements ActionListener, Runnable {

        public void actionPerformed(ActionEvent e) {

            Thread thread = new Thread(this);
            thread.start();

        }

        public void run() {
            
            for (int j = 0; j < 13; j++) {
                for (int i = 0; i < 275; i++) {
                    x1[j] += 2;
                    x2[j] -= 2;
                    y3[j]--;
                    y4[j]++;
                    panel_.repaint();
                    try {
                        Thread.sleep(1);
                    } catch (Exception ex) {
                    }
                }
                
            }

            for (int j = -6; j < 7; j++) {
                for (int i = 0; i < 37; i++) {
                    y1[j + 6] = y1[j + 6] + j * 1 - 1;
                    y2[j + 6] = y2[j + 6] - j * 1 - 1;
                    x3[j + 6] = x3[j + 6] + j * 1;
                    x4[j + 6] = x4[j + 6] - j * 2;
                    panel_.repaint();
                    try {
                        Thread.sleep(1);
                    } catch (Exception ex) {
                    }
                }
            }
        }
    }

    class MyPanel extends JPanel {

        public void paintComponent(Graphics g) {

            g.setColor(Color.white);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());

            Icon CardStatic = new ImageIcon("images/ND.jpg");
            JLabel LabelCardStatic = new JLabel(CardStatic);
            LabelCardStatic.setBounds(550, 300, 120, 140);

            Icon Card1[], Card2[], Card3[], Card4[];
            Card1 = new Icon[13];
            Card2 = new Icon[13];
            Card3 = new Icon[13];
            Card4 = new Icon[13];

            JLabel LabelCard1[], LabelCard2[], LabelCard3[], LabelCard4[];
            LabelCard1 = new JLabel[13];
            LabelCard2 = new JLabel[13];
            LabelCard3 = new JLabel[13];
            LabelCard4 = new JLabel[13];

            for (int i = 0; i < 13; i++) {
                Card1[i] = new ImageIcon("images/ND.jpg");
                LabelCard1[i] = new JLabel(Card1[i]);
                LabelCard1[i].setBounds(x1[i], y1[i], 90, 110);


                Card2[i] = new ImageIcon("images/ND.jpg");
                LabelCard2[i] = new JLabel(Card2[i]);
                LabelCard2[i].setBounds(x2[i], y2[i], 90, 110);

                Card3[i] = new ImageIcon("images/ND.jpg");
                LabelCard3[i] = new JLabel(Card3[i]);
                LabelCard3[i].setBounds(x3[i], y3[i], 90, 110);

                Card4[i] = new ImageIcon("images/ND.jpg");
                LabelCard4[i] = new JLabel(Card4[i]);
                LabelCard4[i].setBounds(x4[i], y4[i], 90, 110);
            }

            panel_.removeAll();
            for (int i = 0; i < 13; i++) {
                panel_.add(LabelCard1[i]);
                panel_.add(LabelCard2[i]);
                panel_.add(LabelCard3[i]);
                panel_.add(LabelCard4[i]);
            }

        }

    }

    public void actionPerformed(ActionEvent e) {

        String strActionCommand = e.getActionCommand();

        if (strActionCommand.equals("Start")) {
           control.SP.StartGame();
        }
        if (strActionCommand.equals("Exit")) {
            
        }
    }

    /*public void MoveImage() {
    
    // Container container = mywind.getContentPane();
    //container.setLayout(null);
    //canvas = new ShowCanvas();
    //container.add(canvas);
    
    //tao 13 quan cho ben trai
    for (int i = 0, j = 130; i < 13; i++, j += 30) {
    t[i] = new JLabel(ic);
    velabai(container, 60, j, 100, 75, t[i]);
    }
    //tao 13 quan cho doi dien
    for (int i = 0, j = 350; i < 13; i++, j += 40) {
    o[i] = new JLabel(ic1);
    velabai(container, j, 60, 75, 100, o[i]);
    }
    //tao 13 quan cho ben phai
    for (int i = 0, j = 130; i < 13; i++, j += 30) {
    p[i] = new JLabel(ic);
    velabai(container, 1100, j, 100, 75, p[i]);
    }
    
    //container.remove(t[1]);
    //container.remove(p[10]);
    //container.remove(o[5]);
    tapbai[0] = "10C";
    tapbai[1] = "2C";
    tapbai[2] = "1H";
    tapbai[3] = "QB";
    tapbai[4] = "JB";
    tapbai[5] = "KR";
    tapbai[6] = "8C";
    tapbai[7] = "4H";
    tapbai[8] = "5B";
    tapbai[9] = "5C";
    tapbai[10] = "2B";
    tapbai[11] = "4R";
    tapbai[12] = "JR";
    
    for (int i = 0, j = 300 + 40 * 13; i < 13; i++, j -= 40) {
    Container main = new MainWind(tapbai[i], container, j);
    container.add(main);
    
    }*/
//        Container main=new MainWind("images/10C.jpg",container,350,600);
    //main.setBounds(350,600,1100, 770);
    //myclass mc=new myclass(container,main);
    //main.addMouseListener(mc);
//        container.add(main);
///        Container main1=new MainWind("images/10B.jpg",container,390,600);
    //main1.setBounds(390,600,75,140);
//        container.add(main1);
    // mywind.setSize(1278, 770);
    // mywind.setVisible(true);
}

/*  public void velabai(Container ss, int x, int y, int w, int h, JLabel t12) {
t12.setBounds(x, y, w, h);
t12.setBorder(blackline);
ss.add(t12);
}

public void xoalabai(Container ss, JLabel t12) {
ss.remove(t12);
}

public void xoabaidanh(Container ss, Container aa) {
ss.remove(aa);
}
}*/
class MainWind extends JPanel implements MouseListener {

    int x, y;
    BufferedImage image;
    Container s;

    public MainWind(String s, Container sss, int i) {
        this.s = sss;
        setLayout(null);
        //  setBackground(Color.red);
        s = "images/" + s + ".jpg";
        setSize(1178, 770);

        //ss2.addMouseMotionListener(new MouseMotionHandler());
        Image img = getToolkit().getImage(s);

        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 1);
        try {
            mt.waitForAll();
        } catch (Exception e) {
            System.out.println("Image not found.");
        }
        image = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB);
        //image = new BufferedImage(20,20,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        addMouseListener(this);
        g2.drawImage(img, 0, 0, this);
        setBounds(i, 600, 75, 100);

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.drawImage(image, x, y, this);
    }

    public void mouseClicked(MouseEvent e) {

        x = 600;
        y = 450;
        //s.remove(this);
        JLabel ss = new JLabel("do ngu");

        //repaint();
        setBounds(0, 0, 1278, 770);
        //s.add(ss);
        s.add(this);


        System.out.print("out put");
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        //x = e.getX() ;
        //y = e.getY() -40;
        //repaint();
    }

    public void mouseExited(MouseEvent e) {
        // x = e.getX() ;
        // y = e.getY() + 40;
        // repaint();
    }
}

/*
class ShowCanvas extends JPanel {
int x, y;
BufferedImage image;

ShowCanvas()
{
setBackground(Color.white);
setSize(600, 600);
//addMouseMotionListener(new MouseMotionHandler());
addMouseListener(new MouseEvent(this);
Image img = getToolkit().getImage("E://hinh anh duy/jess.jpg");

MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Image not found.");
}
image = new BufferedImage(img.getWidth(this), img.getHeight(this),BufferedImage.TYPE_INT_ARGB);
//image = new BufferedImage(20,20,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img, 0, 0, this);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, x, y, this);
}

class MouseEvent extends MouseEvent
{
public void mouseClicked(MouseEvent e)
{
x = 300;
y = 300;
repaint();
}
}
class MouseMotionHandler extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e) {
x = e.getX() +40;
y = e.getY() +40;
repaint();
}

public void mouseMoved(MouseEvent e)
{
}

}
}*/
